/****************************************************************************
**
** Copyright (C) 2018 Alexander Akulich <akulichalexander@gmail.com>
**
** Sailfish Silica UI is under copyright of Jolla Ltd.
**
** Based on Qt Material style
**
****************************************************************************/

import QtQuick 2.9
import QtQuick.Templates 2.2 as T
import QtQuick.Controls.Silica 2.0
import QtQuick.Controls.Silica.impl 2.0

import QtQuick.Controls.Material 2.0 as M
import QtQuick.Controls.Material.impl 2.0 as M

T.SwitchDelegate {
    id: control

    implicitWidth: Math.max(background ? background.implicitWidth : 0,
                            contentItem.implicitWidth + leftPadding + rightPadding)
    implicitHeight: Math.max(background ? background.implicitHeight : 0,
                             Math.max(contentItem.implicitHeight,
                                      indicator ? indicator.implicitHeight : 0) + topPadding + bottomPadding)
    baselineOffset: contentItem.y + contentItem.baselineOffset

    padding: 16
    topPadding: 8
    bottomPadding: 8
    spacing: 16

    indicator: SwitchIndicator {
        x: text ? (control.mirrored ? control.leftPadding : control.width - width - control.rightPadding) : control.leftPadding + (control.availableWidth - width) / 2
        y: control.topPadding + (control.availableHeight - height) / 2
        control: control
    }

    contentItem: Text {
        leftPadding: !control.mirrored ? 0 : control.indicator.width + control.spacing
        rightPadding: control.mirrored ? 0 : control.indicator.width + control.spacing

        text: control.text
        font: control.font
        color: control.enabled ? control.Silica.foreground : control.Silica.hintTextColor
        elide: Text.ElideRight
        verticalAlignment: Text.AlignVCenter
    }

    background: Rectangle {
        implicitHeight: 48

        color: control.highlighted ? control.Silica.listHighlightColor : "transparent"

        M.Ripple {
            width: parent.width
            height: parent.height

            clip: visible
            pressed: control.pressed
            anchor: control
            active: control.down || control.visualFocus || control.hovered
            color: control.Silica.rippleColor
        }
    }
}
